package structure;

import java.util.ArrayList;

import algorithm.miscellaneous;

public class item {

	private String leftside;
	private String rightside;
	private ArrayList<String> lookahead;
	private String afterDot;
	private boolean isComplete;
	
	public item(){

		this.leftside= null;
		this.rightside= null;
		this.lookahead= null;
		this.afterDot= null;
		this.isComplete= false;
	}
	
	public item(String left, String right, ArrayList<String> look){
		
		this.leftside= left;
		this.rightside= right;
		if(look == null)
			this.lookahead= new ArrayList<String>();
		else
			this.lookahead= look;

		if(right.lastIndexOf(".") == right.length()-1)
			this.isComplete= true;
		else
			this.isComplete= false;
		
		this.afterDot= "";
		for(int k=0; k<this.rightside.length()-1; k++){
			
			String tmpS= this.rightside.substring(k, k+1);
			if(tmpS.equals(".")){
				this.afterDot= this.rightside.substring(k+1, k+2);
				break;
			}
		}
	}
	
	//get
	public String getRightside(){
		
		return this.rightside;
	}
	
	public String getLeftside(){
		
		return this.leftside;
	}
	
	public ArrayList<String> getLookahead(){
		
		return this.lookahead;
	}
	
	public String getAfterDot(){
		
		return this.afterDot;
	}
	
	public boolean isComplete(){
		
		return this.isComplete;
	}
	
	public String getRightsideClean(){
		
		char[] tmpC= this.rightside.toCharArray();
		char[] toAdd= new char[tmpC.length];
		for(int k=0; k<tmpC.length; k++){
			
			char c= tmpC[k];
			if(c != '.')
				toAdd[k]= c;
		}
		
		String toReturn= new String(toAdd);
		
		return toReturn;
	}
	//
	
	//set
	public void setLookahead(ArrayList<String> look){
		
		miscellaneous.addAllOnce(look, this.lookahead);
	}
	//
	
	public boolean sameLookahead(ArrayList<String> lookahead){
		
		if(this.lookahead.size() != lookahead.size()) 
			return false;
		
			/*for(int i=0; i<this.lookahead.size(); i++){
	
				if(!this.lookahead.get(i).equals(lookahead.get(i))){
					
					return false;
				}
			}*/
			return this.lookahead.containsAll(lookahead);
		
		
	}
	
	public boolean sameAs(item item){
		
		//stesso leftside
		if(this.leftside.equals(item.getLeftside())){
			//stesso rightside
			if(this.rightside.equals(item.getRightside())){
				//stesso lookahead
				if(this.sameLookahead(item.getLookahead())){
					return true;
				}
			}
		}
		
		return false;
	}
	
	public void printItAll(){
		
		System.out.println("(" + this.leftside + "->" + this.rightside + ", " + this.lookahead + ")");
	}
}